﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Composite.Data;
using System.Web.Script.Serialization;
using LCA.Common.Extensions;

public partial class Admin_LCASystem_Controls_InputOutput : System.Web.UI.UserControl
{

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public void RefreshComponents(Guid? SystemId)
    {
        ctlInputModal.RefreshComponents(SystemId);

        // Load Input into js variables
        using (DataConnection conn = new DataConnection())
        {
            

            if (!SystemId.HasValue)
                return;

            // Input
            var list = from input in conn.Get<LCA.Data.LCASystemInput>()
                       join f in conn.Get<LCA.Data.Flow>() on input.RefItemId equals f.Id into fJoined
                       from ff in fJoined.DefaultIfEmpty()
                       join s in conn.Get<LCA.Data.LCASystem>() on input.RefItemId equals s.Id into sJoined
                       from ss in sJoined.DefaultIfEmpty()
                       join u in conn.Get<LCA.Data.Unit>() on input.Unit equals u.Id into joined
                       from j in joined.DefaultIfEmpty()
                       where input.RelatedSystem == SystemId.Value
                       select new {
                            item_id = input.RefItemId,
                            item_type = input.RefItemType,
                            item_name = ff != null ? ff.Name : ss.Name,
                            amount = input.Amount,
                            unit_id = input.Unit,
                            unit_name = j.Name,
                       };

            list = list.OrderBy(x => x.item_name);

            txtInput.Text = new JavaScriptSerializer().Serialize(list.ToList());

            // Output
            var listOutput = from input in conn.Get<LCA.Data.LCASystemOutput>()
                       join f in conn.Get<LCA.Data.Flow>() on input.RefItemId equals f.Id into fJoined
                       from ff in fJoined.DefaultIfEmpty()
                       join s in conn.Get<LCA.Data.LCASystem>() on input.RefItemId equals s.Id into sJoined
                       from ss in sJoined.DefaultIfEmpty()
                       join u in conn.Get<LCA.Data.Unit>() on input.Unit equals u.Id into joined
                       from j in joined.DefaultIfEmpty()
                       where input.RelatedSystem == SystemId.Value
                       select new
                       {
                           item_id = input.RefItemId,
                           item_type = input.RefItemType,
                           item_name = ff != null ? ff.Name : ss.Name,
                           amount = input.Amount,
                           unit_id = input.Unit,
                           unit_name = j.Name,
                       };

            listOutput = listOutput.OrderBy(x => x.item_name);

            txtOutput.Text = new JavaScriptSerializer().Serialize(listOutput.ToList());
        }

        
    }

    public void SaveData(Guid SystemId)
    {
        // Input
        var table = new JavaScriptSerializer().Deserialize<dynamic>(txtInput.Text);
        
        // Output
        var tableOutput = new JavaScriptSerializer().Deserialize<dynamic>(txtOutput.Text);
        
        using (DataConnection connection = new DataConnection())
        {
            ////////////////////////////// Input
            var deletedList = from i in connection.Get<LCA.Data.LCASystemInput>()
                              where i.RelatedSystem == SystemId
                              select i;

            foreach (LCA.Data.LCASystemInput item in deletedList)
            {
                connection.Delete(item);
            }

            if (table != null)
            {
                LCA.Data.LCASystemInput input = null;
                foreach (var row in table)
                {
                    string item_id = row["item_id"].ToString();
                    string item_type = row["item_type"].ToString();

                    string amount = row["amount"].ToString();
                    string unit_id = row["unit_id"];

                    input = connection.CreateNew<LCA.Data.LCASystemInput>();

                    input.RefItemId = item_id.ToGuid().Value;
                    input.RefItemType = item_type;
                    input.Amount = amount.ToString();
                    input.Unit = unit_id.ToGuid();
                    input.RelatedSystem = SystemId;

                    connection.Add(input);
                }
            }
            


            ////////////////////////////// Output
            var deletedOutputList = from i in connection.Get<LCA.Data.LCASystemOutput>()
                              where i.RelatedSystem == SystemId
                              select i;

            foreach (LCA.Data.LCASystemOutput item in deletedOutputList)
            {
                connection.Delete(item);
            }

            if (tableOutput != null)
            {
                LCA.Data.LCASystemOutput output = null;
                foreach (var row in tableOutput)
                {
                    string item_id = row["item_id"].ToString();
                    string item_type = row["item_type"].ToString();

                    string amount = row["amount"].ToString();
                    string unit_id = row["unit_id"];

                    output = connection.CreateNew<LCA.Data.LCASystemOutput>();

                    output.RefItemId = item_id.ToGuid().Value;
                    output.RefItemType = item_type;
                    output.Amount = amount.ToString();
                    output.Unit = unit_id.ToGuid();
                    output.RelatedSystem = SystemId;

                    connection.Add(output);
                }
            }
            
        }

    }
}